<?php
/**
 * Created by PhpStorm.
 * User: liuyang
 * Date: 2018/9/29
 * Time: 14:14
 */

//密码加密
function hexPass($pass){
    $ckey = config('zhibo.authkey');
    $chairmansourcepwd= $pass;
    $ChairmanPWD = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $ckey, $chairmansourcepwd, MCRYPT_MODE_ECB);
    return bin2hex($ChairmanPWD);
}


function get_redirect_url($url, $referer='', $timeout = 10) {
    $redirect_url = false;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   if ($referer) {
     curl_setopt($ch, CURLOPT_REFERER, $referer);//设置referer
   }
   $content = curl_exec($ch);
   if(!curl_errno($ch)) {
     $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址
   }

   return $redirect_url;
}


//加密
function bhmd5($str = '', $type = false, $time = 0, $key = ''){
    if($str == ''){
        return false;
    }
    $type = ($type) ? 'DECODE' : 'ENCODE';
    return model('tool')->bhmd5($str, $type, $key, $time);
}

function curl_post($url = '', $postdata = '', $options = array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    if(!empty($options)){
        curl_setopt_array($ch, $options);
    }
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function curl_get($url = '', $options = array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    if(!empty($options)){
        curl_setopt_array($ch, $options);
    }
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}